package com.wanli.storemobile.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.storemobile.R;
import com.wanli.storemobile.bean.DeviceListBean;
import com.wanli.storemobile.utils.DateTimeUtil;
import com.wanli.storemobile.utils.NumberFormatUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.ViewHolder> {


    private List<DeviceListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private onDeviveCallBackListener onCallBackListener;

    public DeviceListAdapter(List<DeviceListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(onDeviveCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_device_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DeviceListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvDeviceNo.setText(dataBean.getPrefix() + dataBean.getNo());
        //1品生小白盒，2锦鸿霖，3开店宝码牌4云喇叭5NFC标签卡6码牌音箱7POS机,8极速码,9商米蜻蜓二代
        holder.tvDeviceType.setTextSize(16);
        holder.tv_information.setVisibility(View.GONE);
        holder.tvJihuoStatus.setVisibility(View.VISIBLE);
        holder.ll_is_fanxian.setVisibility(View.VISIBLE);
        holder.ll_order_money.setVisibility(View.VISIBLE);
        if (dataBean.getType() == 1) {
            holder.tvDeviceType.setText("扫码盒");
        } else if (dataBean.getType() == 2) {
            holder.tvDeviceType.setText("锦鸿霖");
        } else if (dataBean.getType() == 3) {
            holder.tvDeviceType.setText("码牌");
            holder.tv_information.setVisibility(View.VISIBLE);
        } else if (dataBean.getType() == 4) {
            holder.tvDeviceType.setText("云音响");
        } else if (dataBean.getType() == 5) {
            holder.tvDeviceType.setTextSize(14);
            holder.tvDeviceType.setText("NFC标签卡");
        } else if (dataBean.getType() == 6) {
            holder.tv_information.setVisibility(View.VISIBLE);
            holder.tvDeviceType.setText("码牌音响");
        } else if (dataBean.getType() == 7) {
            holder.tvDeviceType.setText("POS机");
        } else if (dataBean.getType() == 8) {
            holder.tvDeviceType.setText("收款码");
            holder.tv_information.setVisibility(View.VISIBLE);
            holder.tvJihuoStatus.setVisibility(View.GONE);
            holder.ll_is_fanxian.setVisibility(View.GONE);
            holder.ll_order_money.setVisibility(View.GONE);
        } else if (dataBean.getType() == 9) {
            holder.tvDeviceType.setTextSize(13);
            holder.tvDeviceType.setText("商米蜻蜓二代");
        }
        holder.tvAgent.setText(dataBean.getBind_agent_name());
        if (dataBean.getStatus() == 0) {
            holder.tvCurrentStatus.setText("未使用");
            holder.tvCurrentStatus.setTextColor(context.getResources().getColor(R.color.text_color_2));
        } else if (dataBean.getStatus() == 1) {
            holder.tvCurrentStatus.setText("已使用");
            holder.tvCurrentStatus.setTextColor(context.getResources().getColor(R.color.theme_color));
        } else if (dataBean.getStatus() == 1) {
            holder.tvCurrentStatus.setText("已禁用");
            holder.tvCurrentStatus.setTextColor(context.getResources().getColor(R.color.text_color_2));
        }
        holder.tvJihuoStatus.setText(dataBean.getIs_enable() == 0 ? "未激活" : "已激活");
        if (dataBean.getIs_bind_bonus() == 0) {
            holder.tvIsFanxian.setText("否");
        } else {
            holder.tvIsFanxian.setText("是");
        }
        holder.tv_store_name.setText(dataBean.getBind_store_name());
        String money = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getStore_order_fee()) / 100);
        holder.tvOrderMoney.setText(money + "元");
        holder.tvUsedTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getUsed_time()) * 1000)));
        holder.tv_information.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_device_no)
        TextView tvDeviceNo;
        @BindView(R.id.tv_device_type)
        TextView tvDeviceType;
        @BindView(R.id.tv_agent)
        TextView tvAgent;
        @BindView(R.id.tv_current_status)
        TextView tvCurrentStatus;
        @BindView(R.id.tv_jihuo_status)
        TextView tvJihuoStatus;
        @BindView(R.id.tv_is_fanxian)
        TextView tvIsFanxian;
        @BindView(R.id.tv_order_money)
        TextView tvOrderMoney;
        @BindView(R.id.tv_used_time)
        TextView tvUsedTime;
        @BindView(R.id.tv_information)
        TextView tv_information;
        @BindView(R.id.tv_store_name)
        TextView tv_store_name;
        @BindView(R.id.ll_is_fanxian)
        LinearLayout ll_is_fanxian;
        @BindView(R.id.ll_order_money)
        LinearLayout ll_order_money;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onDeviveCallBackListener {
        void onCallBack(int type, int position);
    }
}
